package com.novel.controller.other;


import com.novel.mapper.SysLogMapper;
import com.novel.pojo.system.SysLog;
import com.novel.pojo.system.User;
import com.novel.service.SysLogService;
import com.novel.utils.FileUploadUtil;
import com.novel.utils.ParseRealIpUtils;
import com.novel.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@Slf4j
@Controller
public class TestController {

    @Autowired
    private FileUploadUtil fileUploadUtil;

    @Autowired
    private HttpSession session;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private SysLogService logService;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private SysLogMapper sysLogMapper;

    @GetMapping("aaa")
    @ResponseBody
    public List<Map<String,Object>> get(){
        return sysLogMapper.getSystemPowerMap();
    }


    @GetMapping("redis/ins")
    @ResponseBody
    public String redis() {

        redisUtils.stringSet("novel-id-1231293712983719283719", 1000 + "", 60 * 60 * 4);
        redisUtils.incrSum("novel-id-1231293712983719283719");
        redisUtils.incrSum("novel-id-1231293712983719283719");
        redisUtils.incrSum("novel-id-1231293712983719283719");
        redisUtils.incrSum("novel-id-1231293712983719283719");
        redisUtils.incrSum("novel-id-1231293712983719283719");
        redisUtils.incrSum("novel-id-1231293712983719283719");
        redisUtils.incrSum("novel-id-1231293712983719283719");
        redisUtils.incrSum("novel-id-1231293712983719283719");
        redisUtils.incrSum("novel-id-1231293712983719283719");
        redisUtils.incrSum("novel-id-1231293712983719283719");
        redisUtils.incrSum("novel-id-1231293712983719283719");
        System.out.println(redisUtils.stringGet("novel-id-1231293712983719283719"));

        return "success";
    }


    @GetMapping("/test/upload/file")
    public ModelAndView mv(ModelAndView mv) {
        mv.setViewName("testupload");
        return mv;
    }

    @PostMapping("test/upload")
    @ResponseBody
    public String upload(List<MultipartFile> fileList) throws Exception {
        for (MultipartFile file : fileList) {
            String upload = fileUploadUtil.upload(file);
            System.out.println(upload);
        }
        return "success";
    }

    @GetMapping("test/log")
    @ResponseBody
    @Deprecated
    public String log() {
        SysLog log = new SysLog();
        log.setId(UUID.randomUUID().toString());
        //获取用户信息
        Object object = session.getAttribute("loginUser");
        if (Objects.nonNull(object)) {
            User user = (User) object;
            //保存用户信息到log中
            log.setUserId(user.getUserId());
        }
        //设置请求ip
        log.setUserIp(ParseRealIpUtils.parseRealIpAddress(request));
        //设置时间
        log.setTime(new Date());
        //记录调用的方法签名
//        log.setMethod(proceedingJoinPoint.getSignature().getName());
        //记录是那个控制器的方法
//        log.setAction(proceedingJoinPoint.getTarget().getClass().getName());
        //设置
        log.setSuccess(1);

        logService.insertSystemSysLog(log);

        return "success";
    }

}